package com.hydl.power.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.CameraService;
import com.hydl.power.utils.TableUtils;
import com.hydl.power.utils.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CameraServiceImpl implements CameraService {
    @Autowired
    private CameraMapper cameraMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private PowerRoomMapper powerRoomMapper;
    /*
    * 分页list
    * */
    @Override
    public PageInfo<Camera> queryAllByPage(Integer pageNum,Integer pageSize,String projectId,String cityId,String roomId,String token) {
      SysUser sysUser = sysUserMapper.getUserByToken(token);
      if(sysUser!=null){
        if(sysUser.getStatus() == 2 ){
          List<Camera> list = cameraMapper.queryAllByPage(pageNum,pageSize,sysUser.getProjectId().toString(),cityId,roomId);
        }
      }
      List<Camera> list = cameraMapper.queryAllByPage(pageNum,pageSize,projectId,cityId,roomId);
      //调用第三方视频接口获取accessToken"
      JSONObject json = new JSONObject();
      json.put("appKey", "bd0f7409b8ac461c8fe5eeb902695702");
      json.put("appSecret", "27d0a827bac32bd79df7e0c1eebbfe98");
      String post = HttpUtil.post("https://open.ys7.com/api/lapp/token/get", json);
      JSONObject jsStr = JSONObject.parseObject(post);
      String accessToken = jsStr.getJSONObject("data").getString("accessToken");
      System.out.println(accessToken);


      for (Camera camera : list) {
        String str = camera.getURL()+camera.getCameraDeviceSerial()+"/1.hd.live&autoplay=1&accessToken="+accessToken;
        camera.setURL(str);
      }
      return new PageInfo<>(list);
    }

    @Override
    public Camera selectById(Integer id) {
        return cameraMapper.selectById(id);
    }

    @Override
    @Transactional
    public int insert(String cityId,String projectId ,String roomId ,String cameraName,String cameraDeviceSerial,String validateCode ) {
      City city=cityMapper.queryCityByCityId(cityId);
      Project project=projectMapper.queryProjectByProId(projectId);
      PowerRoom powerRoom=powerRoomMapper.selectByRoomId(roomId);
      int count = cameraMapper.queryCamera(cameraDeviceSerial);
       Camera camera = new Camera();
      camera.setCityId(cityId);
      camera.setCityName(city.getCityName());
      camera.setProjectId(projectId);
      camera.setProjectName(project.getProjectName());
      camera.setRoomId(roomId);
      camera.setRoomName(powerRoom.getRoomName());
      camera.setCameraName(cameraName);
      camera.setCameraDeviceSerial(cameraDeviceSerial);
      camera.setURL("https://open.ys7.com/ezopen/h5/iframe_se?url=ezopen://open.ys7.com/");

      //调用第三方视频接口获取accessToken"
      JSONObject json = new JSONObject();
      json.put("appKey", "bd0f7409b8ac461c8fe5eeb902695702");
      json.put("appSecret", "27d0a827bac32bd79df7e0c1eebbfe98");
      String post = HttpUtil.post("https://open.ys7.com/api/lapp/token/get", json);
      JSONObject jsStr = JSONObject.parseObject(post);
      String accessToken = jsStr.getJSONObject("data").getString("accessToken");

      JSONObject jsons = new JSONObject();
      jsons.put("accessToken",accessToken);
      jsons.put("deviceSerial",cameraDeviceSerial);
      jsons.put("validateCode",validateCode);
      String posts = HttpUtil.post("https://open.ys7.com/api/lapp/device/add", jsons);
      JSONObject jsStrs = JSONObject.parseObject(posts);
      String code=jsStrs.getString("code");
      System.out.println(code);
      System.out.println(count);
      if(code.equals("200") && count == 0){
         return cameraMapper.insert(camera);
      }else if(code.equals("20017") && count == 0){
        return cameraMapper.insert(camera);
      }else {
        return Integer.parseInt(code);
      }
    }

    @Override
    public int deleteById(Integer id) {
      Camera camera=cameraMapper.selectById(id);
      System.out.println(camera.getCameraDeviceSerial());
      if(camera.getCameraDeviceSerial()==null){
        return 10001;
      }
      //调用第三方视频接口获取accessToken"
      JSONObject json = new JSONObject();
      json.put("appKey", "bd0f7409b8ac461c8fe5eeb902695702");
      json.put("appSecret", "27d0a827bac32bd79df7e0c1eebbfe98");
      String post = HttpUtil.post("https://open.ys7.com/api/lapp/token/get", json);
      JSONObject jsStr = JSONObject.parseObject(post);
      String accessToken = jsStr.getJSONObject("data").getString("accessToken");

      //调用第三方视频接口删除设备"
      JSONObject jsons = new JSONObject();
      jsons.put("accessToken", accessToken);
      jsons.put("deviceSerial", camera.getCameraDeviceSerial());
      String posts = HttpUtil.post("https://open.ys7.com/api/lapp/device/delete", jsons);
      JSONObject jsStrs = JSONObject.parseObject(posts);
      String code=jsStrs.getString("code");
      if(code.equals("200")){
        return cameraMapper.deleteByPrimaryKey(id);
      }else {
        return Integer.parseInt(code);
      }
    }

    @Override
    public int updateById(Camera record) {
        return cameraMapper.updateByPrimaryKeySelective(record);
    }
}
